<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-model-item label="票据类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billTypeId">
              <j-dict-select-tag type="list" v-model="model.billTypeId" dictCode="bill_type_id" placeholder="请选择票据类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="票据版本号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="versionId">
              <j-dict-select-tag type="list" v-model="model.versionId" dictCode="mws_t_bv_versions,version_no,id" placeholder="请选择票据版本号" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="发票号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="voucherNo">
              <a-input-number v-model="model.voucherNo" placeholder="请输入发票号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="发票金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="money">
              <a-input-number v-model="model.money" placeholder="请输入发票金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="开票人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operatorId">
              <j-select-user-by-dep v-model="model.operatorId" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="账务日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="financialDate">
              <j-date placeholder="请选择账务日期" v-model="model.financialDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="操作日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operatedTime">
              <j-date placeholder="请选择操作日期" v-model="model.operatedTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="重打次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reprintTimes">
              <a-input-number v-model="model.reprintTimes" placeholder="请输入重打次数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12" >
            <a-form-model-item label="输出方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="biilOutputType">
              <j-dict-select-tag type="list" v-model="model.biilOutputType" dictCode="biil_output_type" placeholder="请选择输出方式" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12" >
            <a-form-model-item label="票据状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billStatusId">
              <j-dict-select-tag type="list" v-model="model.billStatusId" dictCode="bill_status_id" placeholder="请选择票据状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="补打标志" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isReprint">
              <j-dict-select-tag type="list" v-model="model.isReprint" dictCode="yn" placeholder="请选择补打标志" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12" >
            <a-form-model-item label="是否存档" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isArchived">
              <j-dict-select-tag type="list" v-model="model.isArchived" dictCode="yn" placeholder="请选择是否存档" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12" >
            <a-form-model-item label="打印方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billPrintType">
              <j-dict-select-tag type="list" v-model="model.billPrintType" dictCode="bill_print_type"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="票据使用明细" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="bvSubVouchersTable.loading"
          :columns="bvSubVouchersTable.columns"
          :dataSource="bvSubVouchersTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'BvVouchersForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           billTypeId: [
              { required: true, message: '请输入票据类型!'},
           ],
           versionId: [
              { required: true, message: '请输入票据版本号!'},
           ],
           voucherNo: [
              { required: true, message: '请输入发票号!'},
           ],
        },
        refKeys: ['bvSubVouchers', ],
        tableKeys:['bvSubVouchers', ],
        activeKey: 'bvSubVouchers',
        // 票据使用明细
        bvSubVouchersTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '水量',
              key: 'amount',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '收费金额',
              key: 'money',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        url: {
          add: "/bv/bvVouchers/add",
          edit: "/bv/bvVouchers/edit",
          queryById: "/bv/bvVouchers/queryById",
          bvSubVouchers: {
            list: '/bv/bvVouchers/queryBvSubVouchersByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.bvSubVouchersTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.bvSubVouchers.list, params, this.bvSubVouchersTable)
        }
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_NO_PASSED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          bvSubVouchersList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>